<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="标题" prop="p1">
                <el-input v-model="ruleForm2.p1" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="内容" prop="p2">
                <el-input type="textarea" v-model="ruleForm2.p2" auto-complete="off"></el-input>
            </el-form-item>
            <br>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {quillEditor} from 'vue-quill-editor';
    import pub from '../../utils/pub'
    import upload from '@/components/common/Upload'

    export default {
        components: {
            quillEditor,
            upload,
        },
        data: function () {
            return {
                tree: [],
                arr: [],
                roleList: [],
                isHasManager: false,
                admin: {},
                defaultProps: {
                    children: 'children',
                    label: 'privilege_name'
                },
                ruleForm2: {
                    p1: '', // 便民服务信息标题
                    p2: '', // 便民服务信息内容

                },
                rules2: {
                    p1: [
                        {required: true, message: '请输入标题', trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, message: '请输入内容', trigger: 'blur'}
                    ],
                }
            }
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取信息
            getInfo() {
                this.clear();
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '';
                    return;
                }
                this.loadopen();
                // console.log(this.uid)
                this.config.get('/housinggestate/housinggestate/convenience/findById', {id: this.uid}).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        // console.log(res.data);
                        this.page = res.data.convenience;
                        this.ruleForm2.id = this.page.id;
                        this.ruleForm2.p1 = this.page.title;
                        this.ruleForm2.p2 = this.page.content;
                    } else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                });
            },
            // 清空表单
            clear(){
                this.ruleForm2.p1 = '';
                this.ruleForm2.p2 = '';
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        Array.prototype.remove = function(val) {
                            let index = this.indexOf(val);
                            if (index > -1) {
                                this.splice(index, 1);
                            }
                        };
                        let obj = {
                            'c.id': this.uid,
                            'c.title': this.ruleForm2.p1,
                            'c.content': this.ruleForm2.p2,
                        };
                        // console.log(obj);
                        let url = this.uid ? '/housinggestate/housinggestate/convenience/update' : '/housinggestate/housinggestate/convenience/add';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
